package com.toptech.downloadmanager;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import com.toptech.downloadmanager.ToolUtils.ContantsUtils;
import com.toptech.downloadmanager.ToolUtils.lg;
import com.toptech.downloadmanager.download.DownloadTaskManger;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/3/6.
 */

public class RemoteService extends Service{
    private static final String TAG = "RemoteService";
    private Context mContext;
    private DownloadTaskManger mDownloadTaskManager;
    public static final int TASKCOUNT = 2;
    public static List<FileInfo> sFileInfoList = new ArrayList<>();

    public static RemoteCallbackList<ICallback> sCallback;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        sCallback = new RemoteCallbackList<ICallback>();
        return mBinder;
    }

    @Override
    public void onCreate() {
        mContext = this;
        mDownloadTaskManager = DownloadTaskManger.Holder.getInstance();
        mDownloadTaskManager.init(mContext);
        lg.d("remoteservice oncreate");
        super.onCreate();
    }

    private IRemoteService.Stub mBinder = new IRemoteService.Stub() {

        @Override
        public void setTaskMax(int taskcount) throws RemoteException {

            // mDownloadTaskManager.setTaskCount(TASKCOUNT);

        }

        @Override
        public int getTaskMax() throws RemoteException {
            return TASKCOUNT;
        }


        @Override
        public void addTaskList(List<FileInfo> mFileInfoList) throws RemoteException {
            //清空以前的数据
            sFileInfoList.clear();

            List<FileInfo> listinfos = new ArrayList<FileInfo>();
            for (FileInfo fileInfo : mFileInfoList){
                fileInfo.setFileDir(getFileDir());
                fileInfo.setBtnstatus(ContantsUtils.START);
                listinfos.add(fileInfo);
                sFileInfoList.add(fileInfo);
            }
            lg.d("addTaskList");
            mDownloadTaskManager.completedAndStartDownload(listinfos);
        }





        @Override
        public FileInfo getFileInfoStatus() throws RemoteException {
            FileInfo fileInfo = mDownloadTaskManager.getFileinfoStatus();
            return fileInfo;
        }

        @Override
        public int getTaskProgress(long id) throws RemoteException {
            return 0;
        }

        @Override
        public FileInfo getFileInfoSuccess() throws RemoteException {
            FileInfo fileInfo = mDownloadTaskManager.getFileinfoSuccess();
            return fileInfo;
        }

        @Override
        public FileInfo getFileInfoFail() throws RemoteException {
            FileInfo fileInfo = mDownloadTaskManager.getFileinfoFail();
            return fileInfo;
        }

        @Override
        public List<FileInfo> setAllTaskPause() throws RemoteException {
            return mDownloadTaskManager.pauseAllLoadingTask();
        }

        @Override
        public List<FileInfo> setAllTaskStart() throws RemoteException {
            return mDownloadTaskManager.startAllLoadingTask();
        }

        @Override
        public void registerCallback(ICallback cb) throws RemoteException {
            if (cb != null) {
                sCallback.register(cb);
            }
        }

        @Override
        public void unregisterCallback(ICallback cb) throws RemoteException {
            if (cb != null) {
                sCallback.unregister(cb);
            }
        }

        @Override
        public void setFileTaskStatus(FileInfo fileinfo) throws RemoteException {
            fileinfo.setFileDir(getFileDir());
            lg.d("status : "+fileinfo.getBtnstatus());
            if (fileinfo.getBtnstatus() == ContantsUtils.START){
                mDownloadTaskManager.startDownload(fileinfo);
            }else{
                mDownloadTaskManager.pauseDownload(fileinfo.getApkurl());
            }
           /* Intent intent = new Intent(mContext,DownloadService.class);
            intent.putExtra("fileinfo",fileinfo);
            mContext.startService(intent);*/
        }

        @Override
        public void deleteTask(FileInfo fileinfo) throws RemoteException {
            mDownloadTaskManager.deleteTask(fileinfo);
        }



        @Override
        public List<TaskInfo> getAllTask() throws RemoteException {
            return  DataSupport.findAll(TaskInfo.class);
        }

        @Override
        public List<TaskInfo> getNeedDownloadTask() throws RemoteException {

            return DataSupport.where("fileprogress != ?","100").find(TaskInfo.class);
        }

        @Override
        public List<TaskInfo> getFinishedTask() throws RemoteException {
            List<TaskInfo> lists = DataSupport.where("fileprogress = ?","100").find(TaskInfo.class);
            return lists;
        }

    } ;


    /**
     * 获取文件的路径
     * @return
     */
    private String getFileDir(){
        String dir = null;
        if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED){
            dir = mContext.getExternalCacheDir()+"/mydownload/";
        }else {
            dir = mContext.getCacheDir()+"/mydownload/";
        }
        return dir;
    }


    @Override
    public void onDestroy() {
        sCallback.kill();
        super.onDestroy();
        mDownloadTaskManager.onDestory();
    }
}
